<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.appspot.niehonglei.jdo.Index" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.appspot.niehonglei.dao.PMF" %>
<%@ page import="com.appspot.niehonglei.common.Tools" %>
<%
	/**
	 * 
	 */


	if(!"niehonglei".equals(session.getAttribute("logName"))){
		response.sendRedirect("/");
		return;
	}
	if(request.getParameter("new")!=null){
		session.removeAttribute("editIndex");
	}
	/**
	 * 
	 */	
	String footAction = "publish/";
	String pageNo = request.getParameter("_page");
	int intPageNo = 1;
	int recordsPerPage = 5;
	try{
		intPageNo = Integer.parseInt(pageNo);
	} catch (Exception e){
		//e.printStackTrace();
	}
	int totalRecords = Tools.countIndex(null);
	//防止传入的参数为负数
	if(intPageNo <= 0){
		intPageNo = 1;
	}
	
	//计算总页数
	int totalPageNo = 1;		
	if(totalRecords % recordsPerPage == 0){
		totalPageNo = totalRecords / recordsPerPage;
	} else {
		totalPageNo = (totalRecords / recordsPerPage) + 1;
	}
	//防止总记录数为0
	if(totalPageNo == 0){
		totalPageNo = 1;
	}
	//控制是否在正常的上限范围内
	if((intPageNo - 1) * recordsPerPage > totalRecords){
		intPageNo = totalPageNo;
	}
	
	
	//生成页码
	String footCode = "";
	//小于5 直接生成1 2 3 4 5
	if(totalPageNo <= 8){
		for(int i=0;i<totalPageNo;i++){
			footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
		}			
	}
	
	if(totalPageNo > 8 && intPageNo < 5){
		for(int i=0;i<5;i++){
			footCode += "<a href='/" + footAction + "page/"  + (i + 1) + "' class='blackText'>" + (i + 1) + "</a> ";
		}
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 5)/2 + 5) + "' class='blackText'>...</a> ";
		footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";			
	}		
	//页数大于5且当前页大于5小于totalPageNo - 5 显示成 首页 ... 6 7 8 9 10 ... 末页 1 ... 3 4 5 6 7 ... end
	else if(totalPageNo > 8 && intPageNo >= 5 && (intPageNo <= totalPageNo - 4)){
		footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
		footCode += "<a href='/" + footAction + "page/"  + ((intPageNo-2)/2 + 1) + "' class='blackText'>...</a> ";
		for(int i=0;i<5;i++){
			footCode += "<a href='/" + footAction + "page/"  + (intPageNo + i - 2) + "' class='blackText'>" + (intPageNo + i - 2) + "</a> ";
		}
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - intPageNo)/2 + 1 + intPageNo) + "' class='blackText'>...</a> ";
		footCode += " <a href='/" + footAction + "page/"  + totalPageNo + "' class='blackText'>末页</a>";
	}
	
	//页数大于5且当前页大于totalPageNo - 5 显示成 首页 ... 19 20 21 22 23
	else if(totalPageNo > 8 && (intPageNo > totalPageNo - 4)){
		footCode +=  "<a href='/" + footAction + "page/"  + 1 + "' class='blackText'>首页</a> ";
		footCode += "<a href='/" + footAction + "page/"  + ((totalPageNo - 4)/2) + "' class='blackText'>...</a> ";
		for(int i=0;i<5;i++){
			footCode += " <a href='/" + footAction + "page/"  + (totalPageNo + i - 4) + "' class='blackText'>" + (totalPageNo + i - 4) + "</a> ";
		}			
	}
	String range = " range " + (intPageNo - 1) * recordsPerPage + "," + intPageNo * recordsPerPage;	
	/**
	 * 
	 */	
	Index editIndex = (Index)session.getAttribute("editIndex");
	String content = "";
	String title = "";
	String category = "";
	String comeFrom = "";
	String author = "";
	String id = "";
	if(editIndex!=null){
		if(editIndex.getContent()!=null){
			content = editIndex.getContent().getValue();
		}
		title = editIndex.getTitle();
		category = editIndex.getCategory();
		comeFrom = editIndex.getComeFrom();
		author = editIndex.getAuthor();
		if(editIndex.getId()!=null){
			id = editIndex.getId().intValue() + "";
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发表文章</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	font-size: 12px;
	line-height: 20px;
}
.sideTitle {
	font-size: 14px;
	font-weight: bold;
	color: #E25209;
}
.basicInfo {
	color: #999;
}
.whiteText {
	color: #FFF;
}
.blackText {
	color: #000;
}
.title {
	font-size: 18px;
	font-weight: bold;
	color: #E25209;
}

a.whiteText{

}
a{
	text-decoration:none;
}
a:hover{
	text-decoration:underline;
}

-->
</style>
</head>

<body>
  <input type="button" name="publisharticle" id="publisharticle" onclick="location.href='/publish?new'" value="发布文章" disabled="disabled" />
  <input type="button" name="indexrecommend" id="indexrecommend" onclick="location.href='/common?new&type=0'"  value="首页推荐" />
  <input type="button" name="friendlinks"  id="friendlinks" onclick="location.href='/common?new&type=1'"  value="友情链接" />
  <input type="button" name="publisharticle" id="publisharticle" onclick="location.href='/comment'" value="管理评论" />
<font color="#FF0000" style="display:none" id="tip"></font>
<form id="myForm" name="myForm" action="/action/saveIndex" method="post">
  <p><br />
    内容：

      <textarea name="content" id="content" cols="100" rows="15"><%= Tools.dealNull(content)%></textarea>

    <br />
    标题：

      <input name="title" type="text" id="title" size="50" value="<%= Tools.dealNull(title)%>" />

<br />
    分类：
	<select name="category" id="category">
    	<option value=""></option>
   	  <option value="互联网" <% if("互联网".equals(Tools.dealNull(category))) out.print("selected=\"selected\"");%>>互联网</option>
      <option value="Java" <% if("Java".equals(Tools.dealNull(category))) out.print("selected=\"selected\"");%>>Java</option>
      <option value="搜索" <% if("搜索".equals(Tools.dealNull(category))) out.print("selected=\"selected\"");%>>搜索</option>
      <option value="Js" <% if("Js".equals(Tools.dealNull(category))) out.print("selected=\"selected\"");%>>Js</option>
      <option value="生活" <% if("生活".equals(Tools.dealNull(category))) out.print("selected=\"selected\"");%>>生活</option>
    </select>
	<br />
    来源：

      <input name="comeFrom" type="text" id="comeFrom" size="50"  value="<%= Tools.dealNull(comeFrom)%>"/>

    <br />
    作者：

      <input name="author" type="text" id="author" size="50"  value="<%= Tools.dealNull(author)%>"/>
      <input type="hidden" name="id" id="id" value="<%= Tools.dealNull(id)%>"/>

    <br />

      <input type="submit" name="button" id="button" value="提交" /><input type="button" name="new"  id="new" onclick="location.href='/publish?new'" value="新建" /><input type="button" name="button1" value="首页" onclick="location.href='/'"/><input type="button" name="button22" onclick="location.href='/logout'" value="登出" />
    <br />
  </p>
</form>
  <%
  		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + Index.class.getName()
				+ " order by createDate desc" + range;
		List<Index> list = (List<Index>) pm.newQuery(query)
				.execute();
		if(list==null) return;
		for(int i=0;i<list.size();i++){
			Index index = list.get(i);
			out.println("<br>" + index.getId().intValue() + "：" + index.getTitle() + "（创建时间：" + Tools.date2str(index.getCreateDate()) + "，更新时间：" + Tools.date2str(index.getChangeDate()) + "）<input onclick='javascript:location.href=\"/action/deleteIndex&id="+index.getId().intValue()+"\";' type='button' value='删除'/>" + "<input onclick='javascript:location.href=\"/action/editIndex&id="+index.getId().intValue()+"\";' type='button' value='编辑'/><p>" + index.getContent().getValue() + "</p>");
		}
		pm.close();
  %>
  <p>当前是第<%= intPageNo %>页 共<%= totalPageNo %>页 &nbsp;<%= footCode %>
</body>
</html>
